package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.constant.RedisLoginConStant;
import com.dk.pojo.TPerson;
import com.dk.pojo.TRole;
import com.dk.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("user")
public class UserController {
    @Reference
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("toLogin")
    public String login(){
        return "login";
    }

    //跳转查询
    @RequestMapping("toShow")
    public String toShow(){
        return "tperson/show";
    }
    //查询
    @RequestMapping("getInfo")
    @ResponseBody
    public List<TPerson> getInfo(){
        List<TPerson> list= userService.getInfo();
        return list;
    }
    //跳转新增
    @RequestMapping("toAdd")
    public String toAdd(Model model){
        List<TRole> rlist= userService.getRoleInfo();
        model.addAttribute("rlist",rlist);
        return "tperson/add";
    }
    @RequestMapping("addInfo")
    @ResponseBody
    public boolean addInfo(TPerson person, @RequestParam("roleId")Integer[] rids){

        return userService.addInfo(person,rids);
    }
    //回显
    @RequestMapping("toUpdate/{pid}")
    public String toUpdate(@PathVariable("pid")Integer pid,Model model){
        //获取用户信息
        TPerson person=userService.getUserInfoById(pid);
        //获取角色信息
        List<TRole> rlist=userService.getRoleInfo();
        //获取中间表绑定的rid
        List<Integer> ids=userService.getUserRoleInfoById(pid);
        model.addAttribute("person",person);
        model.addAttribute("rlist",rlist);
        model.addAttribute("ids",ids);
        return "tperson/update";
    }
    //修改新增
    @RequestMapping("updateInfo")
    @ResponseBody
    public boolean updateInfo(TPerson person,@RequestParam("roleId")Integer[] rids){
        return userService.updateInfo(person,rids);
    }
    //删除
    @RequestMapping("deleteUserInfo/{pid}")
    @ResponseBody
    public boolean deleteUserInfo(@PathVariable("pid")Integer pid){
        return userService.deleteUserInfo(pid);
    }
    //登录
    @RequestMapping("login")
    public String login(String pname, String pwd,Model model) throws Exception {
        //1.获取subject 对象 auth: 登录有关
        Subject subject= SecurityUtils.getSubject();
        //2.将表单传过来的参数,封装成token : 令牌
        UsernamePasswordToken token =new UsernamePasswordToken(pname,pwd);
        //3.调用subject的login方法执行登录功能
        // shiro是以抛出异常的方式告诉我们账号不存在或者密码错误.
        try{
            subject.login(token);
        }catch (UnknownAccountException e){
            System.out.println("账号不存在");
            model.addAttribute("message","账号不存在");
            return "login";
        }catch (IncorrectCredentialsException e){
            System.out.println("密码错误");
            model.addAttribute("message","密码错误");
            return "login";
        }
        return "redirect:/menu/treeview";
    }
}
